package com.action.log;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.log.LogReply;
import com.business.log.LogReplyBus;
import com.business.user.UserInfoBus;
import com.config.SystemGlobals;
import com.util.MD5;
import com.util.TextTools;

public class ReplyLog extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String contentType = SystemGlobals.getValue("contentType");
		PrintWriter out = response.getWriter();
		response.setContentType(contentType);
		String logNo = request.getParameter("id");
		String nickname = request.getParameter("nickname");
		String password = request.getParameter("password");
		String authenCode = request.getParameter("authencode");
		String replyContent = request.getParameter("replycontent");
		replyContent = TextTools.txt2html(replyContent);
		String passwordMD5 = new MD5().getMD5ofStr(password);
		UserInfoBus uib = new UserInfoBus();
		if (uib.checkLogin(nickname, passwordMD5)) {
			String AuthenCode = (String) request.getSession().getAttribute(
					"AuthenCode");
			if (!authenCode.equals(AuthenCode)) {
				out
						.print("<script>alert(\"验证码有误，请重新输入\");history.back();</script>");
			} else {
				LogReply lr = new LogReply();
				lr.setLogNo(logNo);
				lr.setNickname(nickname);
				lr.setReplyContent(replyContent);
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				lr.setReplyTime(sdf.format(new Date()));
				LogReplyBus lrb = new LogReplyBus();
				lrb.add(lr);
				out
						.print("<script>alert(\"您成功回复了这篇日志\");window.location=\"log/temp/"
								+ logNo + ".html\";</script>");
			}

		}

		else {

			out.print("<script>alert(\"您还未登录哦~\");history.back();</script>");
			out.flush();
			out.close();
		}
	}

}
